package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.PrizeDiamonds;
import com.baomidou.ant.entity.User;
import com.baomidou.ant.service.ILuckDrawService;
import com.baomidou.ant.service.IPrizeDiamondsService;
import com.baomidou.ant.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.baomidou.ant.utils.RateRandomUtils.produceRateRandomNumber;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/prize-diamonds")
public class PrizeDiamondsController {
    @Autowired
    private IPrizeDiamondsService prizeDiamondsService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ILuckDrawService luckdrawService;

    //展示奖池
    @PostMapping("/listAllPrize")
    public Result listAllPrize(){
        List<PrizeDiamonds> list=prizeDiamondsService.list();
        return Result.getSuccess().setData(list);
    }


    //随机抽奖
    @GetMapping("/randomPrize/{userId}")
    public Result randomPrize(@PathVariable Integer userId){
        User user=userService.getById(userId);
        if(user.getDiamonds()<1000){
            return Result.getFailure().setMsg("您的钻石不足!请充值");
        }
        //定义一个30长度的轮播奖池
        List<String> resultPool;
        //随机得到结果
        //定义分割值
        List<Double> separates = new ArrayList<>();
        separates.add(4.0);
        separates.add(8.0);
        separates.add(11.0);
        //定义每个分段的概率
        List<Double> percents = new ArrayList<>();
        percents.add(75.0);
        percents.add(20.0);
        percents.add(4.8);
        percents.add(0.2);
        //得到随机结果
        int result = (int) produceRateRandomNumber(0, 13, separates, percents);
        if(result>12){result=12; }
        if(result<1){result=1;}

        //处理信息的方法，得到轮播奖池
        resultPool=prizeDiamondsService.handleLuckDraw(userId,result);

        //执行奖品发放的方法
        luckdrawService.pullPrize(result,"钻石抽奖",user);

        //扣除抽奖费用
        user.setDiamonds(user.getDiamonds()-1000);
        userService.saveOrUpdate(user);
        
        System.out.println(result);
        return Result.getSuccess().setData(resultPool);
    }

}
